﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-05-15 10:58:47
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	#region OP用户信息表 数据访问层
	/// <summary>
	///OP用户信息表 数据访问层
	/// </summary>	
	internal partial class OpUserInfor:IOpUserInfor
	{		
    
            //连接字符串
            public virtual string ConnectionStringKey
            {
                get
                {
                    return ConstantUtility.ConnectionEjautobaseKey;
                }
            }
        #region GetMaxID
        
    
      
            public int GetMaxID()
            {
                int nReturn=0;
                string szSql=@"SELECT ISNULL(MAX(OpUserID),0)+1
                               FROM [OpUserInfor]";             
                Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
                DbCommand dbc = db.GetSqlStringCommand(szSql);
                try
                {
                    object o = db.ExecuteScalar(dbc);
                    if (o!=null)
                    {
                        nReturn = Convert.ToInt32(o);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nReturn;               
            }
            
      
        
        #endregion
    
		#region Basic CRUD functions
         private OpUserInforInfo GetByReader(IDataReader dr)
         {
            OpUserInforInfo info = new OpUserInforInfo();
            object obj;
            obj=dr["OpUserID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OpUserID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["LoginName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LoginName = obj.ToString();
            }
            obj=dr["PassWord"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.PassWord = obj.ToString();
            }
            obj=dr["OpUserName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OpUserName = obj.ToString();
            }
            obj=dr["ContactMobile"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ContactMobile = obj.ToString();
            }
            obj=dr["State"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.State = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["LoginCount"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LoginCount = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["LastLoginIP"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LastLoginIP = obj.ToString();
            }
            obj=dr["LastLoginDate"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LastLoginDate = ((obj)==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime(obj);
            }
            obj=dr["IsActive"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.IsActive = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["CreateDateTime"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CreateDateTime = ((obj)==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime(obj);
            }
            obj=dr["UpdateTime"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UpdateTime = ((obj)==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime(obj);
            }
            obj=dr["OperationID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OperationID = obj.ToString();
            }
            obj=dr["RoleId"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.RoleId = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            return info;
         }
		public int Insert(OpUserInforInfo info)
		{
        
        
            string strSql=@"INSERT INTO OpUserInfor			
			(			
			[LoginName],
			[PassWord],
			[OpUserName],
			[ContactMobile],
			[State],
			[LoginCount],
			[LastLoginIP],
			[LastLoginDate],
			[IsActive],
			[CreateDateTime],
			[UpdateTime],
			[OperationID],
			[RoleId]
			)
			 VALUES
			(			
			@LoginName,
			@PassWord,
			@OpUserName,
			@ContactMobile,
			@State,
			@LoginCount,
			@LastLoginIP,
			@LastLoginDate,
			@IsActive,
			@CreateDateTime,
			@UpdateTime,
			@OperationID,
			@RoleId
			)
			 SET @OpUserID=@@IDENTITY
			";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddOutParameter(dbc, "@OpUserID",DbType.Int32, sizeof(Int32));  
            db.AddInParameter(dbc, "@LoginName",DbType.String, info.LoginName);
            db.AddInParameter(dbc, "@PassWord",DbType.String, info.PassWord);
            db.AddInParameter(dbc, "@OpUserName",DbType.String, info.OpUserName);
            db.AddInParameter(dbc, "@ContactMobile",DbType.String, info.ContactMobile);
            db.AddInParameter(dbc, "@State",DbType.Int32, info.State);
            db.AddInParameter(dbc, "@LoginCount",DbType.Int32, info.LoginCount);
            db.AddInParameter(dbc, "@LastLoginIP",DbType.String, info.LastLoginIP);
            db.AddInParameter(dbc, "@LastLoginDate",DbType.DateTime, info.LastLoginDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);
            db.AddInParameter(dbc, "@CreateDateTime",DbType.DateTime, info.CreateDateTime.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UpdateTime",DbType.DateTime, info.UpdateTime.ToNullDbDateTime());
            db.AddInParameter(dbc, "@OperationID",DbType.String, info.OperationID);
            db.AddInParameter(dbc, "@RoleId",DbType.Int32, info.RoleId);

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (int)db.GetParameterValue(dbc,"OpUserID");
         
		}
		
		
        public void Update(OpUserInforInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE OpUserInfor SET ");
            sbSql.AppendLine("[LoginName] = @LoginName,");
                
            sbSql.AppendLine("[PassWord] = @PassWord,");
                
            sbSql.AppendLine("[OpUserName] = @OpUserName,");
                
            sbSql.AppendLine("[ContactMobile] = @ContactMobile,");
                
            sbSql.AppendLine("[State] = @State,");
                
            sbSql.AppendLine("[LoginCount] = @LoginCount,");
                
            sbSql.AppendLine("[LastLoginIP] = @LastLoginIP,");
                
            sbSql.AppendLine("[LastLoginDate] = @LastLoginDate,");
                
            sbSql.AppendLine("[IsActive] = @IsActive,");
                
            sbSql.AppendLine("[CreateDateTime] = @CreateDateTime,");
                
            sbSql.AppendLine("[UpdateTime] = @UpdateTime,");
                
            sbSql.AppendLine("[OperationID] = @OperationID,");
                
            sbSql.AppendLine("[RoleId] = @RoleId");
                
            sbSql.Append(" where  [OpUserID] = @OpUserID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@OpUserID",DbType.Int32, info.OpUserID);
            db.AddInParameter(dbc, "@LoginName",DbType.String, info.LoginName);
            db.AddInParameter(dbc, "@PassWord",DbType.String, info.PassWord);
            db.AddInParameter(dbc, "@OpUserName",DbType.String, info.OpUserName);
            db.AddInParameter(dbc, "@ContactMobile",DbType.String, info.ContactMobile);
            db.AddInParameter(dbc, "@State",DbType.Int32, info.State);
            db.AddInParameter(dbc, "@LoginCount",DbType.Int32, info.LoginCount);
            db.AddInParameter(dbc, "@LastLoginIP",DbType.String, info.LastLoginIP);
            db.AddInParameter(dbc, "@LastLoginDate",DbType.DateTime, info.LastLoginDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);
            db.AddInParameter(dbc, "@CreateDateTime",DbType.DateTime, info.CreateDateTime.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UpdateTime",DbType.DateTime, info.UpdateTime.ToNullDbDateTime());
            db.AddInParameter(dbc, "@OperationID",DbType.String, info.OperationID);
            db.AddInParameter(dbc, "@RoleId",DbType.Int32, info.RoleId);
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        public void Delete(int opUserID)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [OpUserInfor]  ");
            strSql.Append("WHERE [OpUserID]=@OpUserID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@OpUserID",DbType.Int32, opUserID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
          
           
		}
        
         public IList<OpUserInforInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<OpUserInforInfo> list = new List<OpUserInforInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "OpUserInfor");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    OpUserInforInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }
        
      
        public OpUserInforInfo GetByID(int opUserID)
		{
            Model.OpUserInforInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [OpUserInfor] ");
            strSql.Append("WHERE [OpUserID]=@OpUserID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@OpUserID",DbType.Int32, opUserID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}
		
        
		#endregion
	}
	#endregion
}